package com.workcenter.controller.Sys;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.Framework.Base.BaseController;
import com.common.annotation.Log;
import com.common.utils.MD5Utils;
import com.common.utils.PageUtils;
import com.common.utils.Query;
import com.common.utils.R;
import com.workcenter.entity.Sys.RoleDO;
import com.workcenter.entity.Sys.SysUserDO;
import com.workcenter.service.RoleService;
import com.workcenter.service.UserService;

@RequestMapping("/sys/user")
@Controller
public class UserController extends BaseController {
  @Autowired
  UserService userService;
  @Autowired
  RoleService roleService;

  @RequiresPermissions("sys:user:user")
  @RequestMapping("")
  String user(Model model) {
    return "sys/user/user";
  }

  @RequestMapping("/list")
  @ResponseBody
  PageUtils list(@RequestParam Map<String, Object> params) {
    // 查询列表数据
    Query query = new Query(params);
    List<SysUserDO> sysUserList = userService.list(query);
    int total = userService.count(query);
    PageUtils pageUtil = new PageUtils(sysUserList, total);
    return pageUtil;
  }

  @RequiresPermissions("sys:user:add")
  @Log("添加用户")
  @RequestMapping("/add")
  String add(Model model) {
    List<RoleDO> roles = roleService.list();
    model.addAttribute("roles", roles);
    return "sys/user/add";
  }

  @RequiresPermissions("sys:user:edit")
  @Log("编辑用户")
  @RequestMapping("/edit/{id}")
  String edit(Model model, @PathVariable("id") Long id) {
    SysUserDO userDO = userService.get(id);
    model.addAttribute("user", userDO);
    List<RoleDO> roles = roleService.list(id);
    model.addAttribute("roles", roles);
    return "sys/user/edit";
  }

  @RequiresPermissions("sys:user:add")
  @Log("保存用户")
  @RequestMapping("/save")
  @ResponseBody
  R save(SysUserDO user) {
    if ("test" == getUsername()) {
      return R.error(1, "演示系统不允许删除,完整体验请部署程序");
    }
    user.setPassword(MD5Utils.encrypt(user.getUsername(), user.getPassword()));
    if (userService.save(user) > 0) {
      return R.ok();
    }
    return R.error();
  }

  @RequiresPermissions("sys:user:edit")
  @Log("更新用户")
  @RequestMapping("/update")
  @ResponseBody
  R update(SysUserDO user) {
    // return R.error(1, "演示系统不允许修改");
    if (userService.update(user) > 0) {
      return R.ok();
    }
    return R.error();
  }

  @RequiresPermissions("sys:user:remove")
  @Log("删除用户")
  @RequestMapping("/remove")
  @ResponseBody
  R remove(Long id) {
    // return R.error("演示系统不允许删除");
    if (userService.remove(id) > 0) {
      return R.ok();
    }
    return R.error();
  }

  @RequiresPermissions("sys:user:batchRemove")
  @Log("批量删除用户")
  @RequestMapping("/batchRemove")
  @ResponseBody
  R batchRemove(@RequestParam("ids[]") Long[] userIds) {
    // return R.error("演示系统不允许删除");
    List<Long> Ids = Arrays.asList(userIds);
    int r = userService.batchremove(Ids);
    System.out.println(r);
    if (r > 0) {
      return R.ok();
    }
    return R.error();
  }

  @RequestMapping("/exit")
  @ResponseBody
  boolean exit(@RequestParam Map<String, Object> params) {
    // Query query = new Query(params);
    return !userService.exit(params);// 存在，不通过，false
  }

  @RequiresPermissions("sys:user:resetPwd")
  @Log("请求更改用户密码")
  @RequestMapping("/resetPwd/{id}")
  String resetPwd(@PathVariable("id") Long userId, Model model) {

    SysUserDO userDO = new SysUserDO();
    userDO.setUserId(userId);
    model.addAttribute("user", userDO);
    return "sys/user/reset_pwd";
  }

  @Log("提交更改用户密码")
  @RequestMapping("/resetPwd")
  @ResponseBody
  R resetPwd(SysUserDO user) {
    if (1L == user.getUserId()) {
      return R.error("演示系统不允许修改管理员密码");
    }
    user.setPassword(MD5Utils.encrypt(userService.get(user.getUserId()).getUsername(),
        user.getPassword()));
    if (userService.resetPwd(user) > 0) {
      return R.ok();
    }
    return R.error();
  }

}
